#include "front/text_normalize.h"

using namespace std;
using namespace ppspeech;

const wregex mcWregexLetterSplit(L"[A-Z]{2,}");

void TextNormalizer::TnReLetterSplit(wstring* fpInWsentence)
{
	wsmatch tWsmatch;
	while (regex_search(*fpInWsentence, tWsmatch, mcWregexLetterSplit)) {
		wstring tWstrReplace = L"";
		wstring tWstrMatch = wstring(tWsmatch[0]);
		string tStrMatch = wstring2utf8string(tWstrMatch);

		auto it = find(VecStr_NotSplitUpperWord.begin(), VecStr_NotSplitUpperWord.end(), tStrMatch);
		if (it != VecStr_NotSplitUpperWord.end()) {
			transform(tStrMatch.begin(), tStrMatch.end(), tStrMatch.begin(), ::tolower);		//大写转小写
			tWstrReplace += utf8string2wstring(tStrMatch);
		}
		else {
			tWstrReplace += L" ";
			for(const wchar_t tWchar : tWstrMatch) {
				tWstrReplace += tWchar;
				tWstrReplace += L" ";
			}
		}

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), tWstrReplace);
	}
}


const wregex mcWregexLetter_a(L"\\s+(A|a)\\s+");
//区分“诶”和“呃”的发音
void TextNormalizer::TnReLetterAa(wstring* fpInWsentence)
{
	wsmatch tWsmatch;
	while( regex_search(*fpInWsentence, tWsmatch, mcWregexLetter_a) ) {
		//前向单词
		wstring tWstrBefore;
		const wchar_t* tp = fpInWsentence->c_str();
		for(int i=tWsmatch.position(0)-1; i>=0; i--) {
			if(  (tp[i] >= L'a' && tp[i] <= L'z') || (tp[i] >= L'A' && tp[i] <= L'Z')  ) {
				tWstrBefore = tp[i] + tWstrBefore;
			}
			else {
				break;
			}
		}
		string tStrBefore = wstring2utf8string(tWstrBefore);
		transform(tStrBefore.begin(), tStrBefore.end(), tStrBefore.begin(), ::tolower);		//大写转小写

		//后向单词
		wstring tWstrAfter;
		for(int i=tWsmatch.position(0)+tWsmatch.length(0); i<fpInWsentence->length(); i++) {
			if(  (tp[i] >= L'a' && tp[i] <= L'z') || (tp[i] >= L'A' && tp[i] <= L'Z')  ) {
				tWstrAfter += tp[i];
			}
			else {
				break;
			}
		}
		string tStrAfter = wstring2utf8string(tWstrAfter);
		transform(tStrAfter.begin(), tStrAfter.end(), tStrAfter.begin(), ::tolower);		//大写转小写

		bool tReadEyFlag = false;
		if(tStrBefore == "an" || tStrBefore == "the") {	//a必定读“诶”
			tReadEyFlag = true;
		}
		else if(tStrBefore == "" && tStrAfter == "") {
			tReadEyFlag = true;
		}

		wstring tWstrReplace = L" `a~ ";	//读“呃”
		if(tReadEyFlag) {
			tWstrReplace = L" a` ";			//读“诶”
		}
		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), tWstrReplace);
	}
}
